package com.miplus.ccrm.core.service.file;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.file.UploadResVo;
import com.miplus.ccrm.core.pojo.FiApplyAtt;
import com.miplus.ccrm.core.mapper.FiApplyAttMapper;
import com.miplus.ccrm.core.utils.EnumConfirmStatus;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【fi_apply_att(申请附件文件表)】的数据库操作Service实现
* @createDate 2024-04-15 20:09:33
*/
@Service
public class FiApplyAttServiceImpl extends MPJBaseServiceImpl<FiApplyAttMapper, FiApplyAtt>
    implements FiApplyAttService{
    @Override
    public int AddApplyFile(Integer applynumber,String applytype,List<UploadResVo> filelist)
    {
        if(JPubUtils.IsEmpty(filelist)) return 0;

        return AddFile(applynumber,applytype,EnumConfirmStatus.Pending.value,filelist);
    }

    @Override
    public int AddResultFile(Integer applynumber,String applytype, List<UploadResVo> filelist)
    {
        if(JPubUtils.IsEmpty(filelist)) return 0;

        return AddFile(applynumber,applytype,EnumConfirmStatus.Finish.value,filelist);
    }

    private int AddFile(Integer applynumber,String applytype,Integer status,List<UploadResVo> filelist)
    {
        for(UploadResVo resVo : filelist){
            FiApplyAtt fiApplyAtt=new FiApplyAtt();
            fiApplyAtt.setApplynumber(applynumber);
            fiApplyAtt.setApplytype(applytype);
            fiApplyAtt.setFilename(resVo.getFilename());
            fiApplyAtt.setApiurl(resVo.getApiurl());
            fiApplyAtt.setFiledir(resVo.getFiledir());
            fiApplyAtt.setIpaddr(resVo.getIpaddr());
            fiApplyAtt.setWeburl(resVo.getWeburl());
            //2024-07-24 为了显示附件原始文件名
            fiApplyAtt.setOrifilename(resVo.getOrifilename());
            fiApplyAtt.setStatus(status);
            fiApplyAtt.setUpdt(new Date());

            baseMapper.insert(fiApplyAtt);
        }
        return filelist.size();
    }

    @Override
    public List<UploadResVo> GetApplyFile(Integer applynumber,String applytype)
    {
        return selectJoinList(UploadResVo.class,
                new MPJLambdaWrapper<FiApplyAtt>()
                        .selectAll(FiApplyAtt.class)
                        .eq("applynumber",applynumber)
                        .eq("applytype",applytype)
                        .orderByAsc(FiApplyAtt::getId)
            );
    }
}




